<template>
  <div class="warp-box" id="upadd">
    <el-row class="search-box">
      <el-col :span="24" class="add-bottom">
        <span class="personal-title" v-text="parames.id?'新建推广链接':'编辑推广链接'"></span>
      </el-col>
    </el-row>
    <div class="main-box">
      <div class="inforlist">
        <div class="inforadd" style="margin-top: 30px;width: 880px">
          <el-form
            :model="parames"
            :rules="rules"
            ref="parames"
            label-width="140px"
            class="demo-ruleForm"
          >
            <el-form-item label="渠道链接名称:" prop="name">
              <el-input style="width:400px;" v-model="parames.name" placeholder="请输入渠道链接名称"></el-input>
            </el-form-item>
            <el-form-item label="推广者:" prop="promoterId">
              <el-select v-model="parames.promoterId" placeholder="请选择推广者">
                <el-option
                  v-for="item in promoterData"
                  :key="item.id"
                  :label="item.name"
                  :value="item.id"
                ></el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="推广页面:" prop="url">
              <el-select v-model="parames.url" placeholder="请选择推广页面">
                <el-option
                  v-for="item in urlData"
                  :key="item.name"
                  :label="item.name"
                  :value="item.url"
                ></el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="结算方式:" prop="tradeMode">
              <el-select v-model="parames.tradeMode" placeholder="请选择结算方式">
                <el-option label value="请选择结算方式"></el-option>
                <el-option label="CPA" value="CPA"></el-option>
                <el-option label="CPS" value="CPS"></el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="单价/比例:" prop="unitPrice">
              <el-input
                style="width:400px;"
                v-model="parames.unitPrice"
                placeholder="请输入CPA单价或CPS佣金比例"
              ></el-input>
            </el-form-item>
            <el-form-item label="最小注册数:" prop="dftRegNum">
              <el-input style="width:400px;" v-model="parames.dftRegNum" placeholder="请输入最小注册数"></el-input>
            </el-form-item>
            <el-form-item label="注册转化率:" prop="regNumRate">
              <el-input
                style="width:400px;"
                v-model="parames.regNumRate"
                type="number"
                placeholder="请输入注册转化率"
              ></el-input>
            </el-form-item>
            <el-form-item label="状态:" prop="status">
              <el-popover
                placement="right"
                title
                offset="300"
                width="200"
                trigger="manual"
                v-model="statusBtn"
                content="温馨提示：选择下线后应用就不会显示在前端界面上！"
              ></el-popover>
              <el-switch
                v-model="parames.status"
                active-value="1"
                inactive-value="0"
                active-text="上线"
                inactive-text="下线"
                @change="changeStatus"
              ></el-switch>
            </el-form-item>
            <el-form-item label="简介:" prop="memo">
              <el-input
                rows="6"
                style="width:400px;"
                type="textarea"
                v-model="parames.memo"
                placeholder="请输入简介"
              ></el-input>
            </el-form-item>
            <el-form-item>
              <el-button round type="primary" @click="handleSend">保存并返回列表</el-button>
              <el-button round @click="resetData">重置</el-button>
            </el-form-item>
          </el-form>
        </div>
      </div>
    </div>
  </div>
</template>
<script>
export default {
  data() {
    return {
      parames: {
        id: "",
        key: "",
        name: "",
        promoterId: "",
        url: "",
        tradeMode: '',
        unitPrice: "",
        dftRegNum: "",
        regNumRate: "",
        status: "0",
        memo: ""
      },
      urlData: [],
      promoterData: [],
      action: "",
      statusBtn: false,
      headers: {
        "X-Requested-With": "XMLHttpRequest"
      },
      moduleKeys: [],
      rules: {
        name: [{ required: true, message: "请输入名称", trigger: "blur" }],
        promoterId: [
          { required: true, message: "请选择推广人", trigger: "blur" }
        ],
        url: [{ required: true, message: "请选择推广页面", trigger: "blur" }],
        unitPrice: [
          { required: true, message: "请输入单价比例", trigger: "blur" }
        ],
        dftRegNum: [
          { required: true, message: "请输入最小注册数", trigger: "blur" }
        ],
        regNumRate: [
          { required: true, message: "请输入注册转化率", trigger: "blur" }
        ]
      }
    };
  },
  methods: {
    changeStatus(val) {
      if (val == 0) {
        this.statusBtn = true;
        setTimeout(() => {
          this.statusBtn = false;
        }, 1000);
      }
    },
    handleSend() {
      let url = "";
      if (this.parames.id) {
        url = "/tx/promote/url/edit";
      } else {
        url = "/tx/promote/url/save";
      }
      this.$refs.parames.validate(valid => {
        if (valid) {
          this.$axios
            .post(url, {
              id: this.parames.id,
              name: this.parames.name,
              promoterId: this.parames.promoterId,
              url: this.parames.url,
              tradeMode: this.parames.tradeMode,
              unitPrice: this.parames.unitPrice,
              dftRegNum: this.parames.dftRegNum,
              regNumRate: this.parames.regNumRate,
              status: this.parames.status,
              memo: this.parames.memo
            })
            .then(res => {
              if (res.code === 10200) {
                this.resetData();
                this.$router.push({ name: "link_list" });
              }
            });
        } else {
          return false;
        }
      });
    },
    render(id) {
      this.$axios.get("tx/promote/url/edit/" + id).then(res => {
        if (res.code === 10200) {
          if (id === "0") {
            this.urlData = res.data.pages;
            this.promoterData = res.data.promoters
          } else {
            this.urlData = res.data.pages;
            this.promoterData = res.data.promoters
            this.parames = res.data;
            this.parames.status = res.data.status + "";
          }
        }
      });
    },
    resetData(formName) {
      this.parames.id = "";
      this.parames.name = "";
      this.parames.url = "";
      this.parames.tradeMode = "";
      this.parames.promoterId = "";
      this.parames.unitPrice = "";
      this.parames.dftRegNum = "";
      this.parames.regNumRate = "";
      this.parames.status = 0;
      this.parames.memo = "";
      this.$refs.parames.resetFields();
    }
  },
  mounted() {
    this.parames.id = this.$route.query.id;
    if (this.parames.id) {
      this.render(this.parames.id);
    } else {
      this.render("0");
    }
  }
};
</script>
<style>
.el-checkbox + .el-checkbox {
  margin: 0;
  padding-right: 30px;
}
</style>
